package edu.gatech.gro.model.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.gatech.gro.model.ListItem;

public class ListItemDao extends DatabaseHandler {

	private static final String TAG = "NAMED_LIST_ITEM_DAO";

	public ListItemDao(Context context) {
		super(context);
	}

	@Override
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(NAMED_LIST_ITEM_TABLE, "namedListId > 0", null);
		db.close();
	}

	public ArrayList<ListItem> getAllListItems(final int listId) {
		ArrayList<ListItem> list = new ArrayList<ListItem>();
		String selectQuery = "SELECT  * FROM " + NAMED_LIST_ITEM_TABLE + " WHERE namedListId = ?";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(listId) });

		if (cursor.moveToFirst()) {
			do {
				ListItem listItem = new ListItem();
				listItem.setNamedListId(cursor.getInt(0));
				listItem.setId(cursor.getInt(1));
				listItem.setChecked(cursor.getInt(2) == 1);
				listItem.setQuantity(cursor.getInt(3));
				listItem.setCurrentQuantity(cursor.getInt(4));
				listItem.setCreationTime(cursor.getInt(5));
				listItem.setLastUpdateTime(cursor.getInt(6));
				listItem.setDeleteFlag(cursor.getInt(7) == 1);
				list.add(listItem);
			} while (cursor.moveToNext());
		}
		db.close();

		return list;
	}

	public int count() {
		String countQuery = "SELECT  * FROM " + NAMED_LIST_ITEM_TABLE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();
	}

	public boolean saveListItem(ListItem listItem) {
		if (listItem == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("namedListId", listItem.getNamedListId());
		values.put("itemId", listItem.getId());
		values.put("checked", listItem.isChecked() ? 1 : 0);
		values.put("quantity", listItem.getQuantity());
		values.put("currentQuantity", listItem.getCurrentQuantity());
		values.put("creationTime", listItem.getCreationTime());
		values.put("lastUpdateTime", listItem.getLastUpdateTime());
		values.put("deleteFlag", listItem.isDeleteFlag() ? 1 : 0);

		long id = db.insert(NAMED_LIST_ITEM_TABLE, null, values);
		if (id == -1) {
			// TODO: error
			return false;
		} else {
			listItem.setId((int) id);
		}

		db.close();
		return true;
	}

	public boolean updateListItem(ListItem listItem) {
		if (listItem == null || listItem.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("checked", listItem.isChecked() ? 1 : 0);
		values.put("quantity", listItem.getQuantity());
		values.put("currentQuantity", listItem.getCurrentQuantity());
		values.put("lastUpdateTime", listItem.getLastUpdateTime());
		values.put("deleteFlag", listItem.isDeleteFlag() ? 1 : 0);

		int r = db.update(NAMED_LIST_ITEM_TABLE, values, "namedListId = ? AND itemId = ?", new String[] { String.valueOf(listItem.getNamedListId()), String.valueOf(listItem.getId()) });
		db.close();
		return r == 1;
	}

	public boolean deleteListItem(ListItem listItem) {
		if (listItem == null || listItem.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		int r = db.delete(NAMED_LIST_ITEM_TABLE, "namedListId = ? AND itemId = ?", new String[] { String.valueOf(listItem.getNamedListId()), String.valueOf(listItem.getId()) });
		db.close();
		return r == 1;
	}
}
